package net.daum.hibernate.sample;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import org.hibernate.SessionFactory;
import org.hibernate.annotations.WhereJoinTable;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

import com.google.gson.annotations.Expose;

@Entity(name = "car_model")
public class Model {
	@Column
	@Id
	@GeneratedValue
	@Expose
	private Integer modelid;

	@Column
	@Expose
	private String year;

	@Column
	@Expose
	private String modelname;

	@ManyToOne
	@JoinColumn(name = "brandid")
	private Brand brand;

	@ManyToMany
	@JoinTable(name = "car_image_map", joinColumns = { @JoinColumn(name = "objectid", referencedColumnName = "modelid") }, inverseJoinColumns = { @JoinColumn(name = "imgid") })
	// @Where(clause = "objectcode = 'MODEL'")
	@WhereJoinTable(clause = "objectcode = 'MODEL'")
	private List<Image> imageList = new ArrayList<Image>();

	public List<Image> getImageList() {
		return imageList;
	}

	public void setImageList(List<Image> imageList) {
		this.imageList = imageList;
	}

	public Integer getModelid() {
		return modelid;
	}

	public void setModelid(Integer modelid) {
		this.modelid = modelid;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getModelname() {
		return modelname;
	}

	public void setModelname(String modelname) {
		this.modelname = modelname;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public static void main(String[] args) {
		SessionFactory sessionFactory = new AnnotationConfiguration()
				.configure("net/daum/hibernate/sample/hibernate.cfg.xml")
				.buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		List<Model> modelList = session.createQuery("from car_model")
				.setFirstResult(20)
				.setMaxResults(10).list();
		
		for (Model model : modelList) {
			Util.print(model);
		}
		

		session.getTransaction().commit();
		session.close();
	}
}
